package tcu.iot.gasmanage.service;

import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tcu.iot.gasmanage.common.utils.DateUtils;
import tcu.iot.gasmanage.common.Result;
import tcu.iot.gasmanage.entity.person.Person;
import tcu.iot.gasmanage.entity.person.PersonQuery;
import tcu.iot.gasmanage.mapper.EquipmentMapper;
import tcu.iot.gasmanage.mapper.PersonMapper;

import javax.annotation.Resource;
import java.util.HashMap;


@Service
@Transactional
public class PersonService {
    @Resource
    private PersonMapper personMapper;
    @Resource
    private EquipmentMapper equipmentMapper;

    public Result getAllPerson(PersonQuery personQuery) {
        PageHelper.startPage(personQuery.getPage(), personQuery.getAmount());
        HashMap<String, Object> hashMap = new HashMap<String, Object>();

        if (personQuery.IsSelectPersonNull(personQuery)) {
            hashMap.put("items", personMapper.SelectPersonByMany(personQuery));
            hashMap.put("total", personMapper.SelectPersonCount(personQuery));
        } else {
            hashMap.put("items", personMapper.getAllPerson(personQuery));
            hashMap.put("total", personMapper.count());
        }

        return Result.ok("true", 200, "成功", hashMap);
    }

    public Result AddPerson(Person person) {
        String time = DateUtils.dealDateFormat(person.getTime());

        person.setTime(time);

        if (personMapper.AddPerson(person)) {
            return Result.ok("true", 200, "人员添加成功！", "人员添加成功！");
        } else
            return Result.failed("false", 401, "人员添加失败！");
    }

    public Result DeletePerson(int id) {
        if (personMapper.DeletePerson(id)) {
            return Result.ok("true", 200, "删除成功！", "删除成功！");
        } else
            return Result.failed("false", 401, "删除失败！");
    }

    public Result UpdatePerson(Person person) {
        String time = DateUtils.dealDateFormat(person.getTime());

        person.setTime(time);

        if (personMapper.UpdatePerson(person)) {
            return Result.ok("true", 200, "信息更新成功！", "信息更新成功！");
        } else
            return Result.failed("false", 401, "信息更新失败！");
    }

    public Result FindOptions() {
        HashMap<String, Object> hashMap = new HashMap<>();

        hashMap.put("limitsOptions", personMapper.FindLimitsOptions());
        hashMap.put("managementOptions", personMapper.FindManagementOptions());
        hashMap.put("pipelineOptions", personMapper.FindPipelineOptions());
        hashMap.put("regionOptions", personMapper.FindRegionOptions());
        hashMap.put("siteOptions", personMapper.FindSiteOptions());

        return Result.ok("true", 200, "成功", hashMap);
    }
}
